package com.liao.ten.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.liao.ten.utils.LoadNetDataAsyncTask;

public abstract class BaseFragment extends Fragment implements LoadNetDataAsyncTask.onGetNetDataListener {
    ProgressDialog dialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void loadData(Context context, String path, int id,int className) {
        LoadNetDataAsyncTask task = new LoadNetDataAsyncTask(context, this, id,className);
        task.execute(path);
    }

    public void loadData(Context context, String path, int id, String fileName) {
        LoadNetDataAsyncTask task = new LoadNetDataAsyncTask(context, this, id, fileName);
        task.execute(path);
    }

    public boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        Network activeNetwork = connectivityManager.getActiveNetwork();
        if (activeNetwork == null) {
            return false;
        }
        return true;
    }

    public void showDialog(Context context) {
        dialog = new ProgressDialog(context);
        dialog.setTitle("提示信息");
        dialog.setMessage("正在加载中......");
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            System.out.println("BaseFragment.dismissDialog1");
        } else {
            System.out.println("BaseFragment.dismissDialog2");
        }
    }

    @Override
    public abstract void showDataFromNet(String s, int id);
}
